package ru.fdooch
{
	//Import
	
	public class CatalogDisplay extends DisplayPanel
	{
		private var handler:RacePadHandler;
		
		private var vb:VBox;
		private var vb_dog:VBox;
		
		
		//For adding dog
		private	var d_id:NumericStepper;
		private	var d_name:InputText;
		private	var d_doc:InputText;
		private	var d_birth:InputText;
		private	var d_owner:InputText;
		private	var d_cat:int;
		private var d_add_button:PushButton;
		private var d_cancel_button:PushButton;
		private var d_cat:int;
		
		
		public function CatalogDisplay (model:RacePadHandler, parent:DisplayObjectContainer=null, xpos:Number=0, ypos:Number=0, legendTxt:String="")
		{
			super (parent, xpos:Number=0, ypos:Number=0, "Catalog. Click 'ADD' button on Category to add dog entry");
			handler = model;
			
			vb = new VBox(scrollDisplay);
			
			drawCategories();
		}
		
		private function drawCategories():void
		{
			legendLabel.text = "Catalog. Click 'ADD' button on Category to add dog entry";
		
			vb = null;
			vb = new VBox(scrollDisplay);
			vb.spacing = Layout.SIDE_MENU_GAP;
			
			for(var i:int=0;i<handler.numCategories;i++)
			{
				var c_window:CategoryWindow = new CategoryWindow(handler, i, vb);
				c_window.addEventListener("wantToAdd",wantToAddHandler);
			}
			
		}
		
		private function wantToAddHandler(e:Event):void
		{
			cw:CategoryWindow = e.currentTarget as CategoryWindow;
			trace("Want to add dog in ("+cw.catId+") "+handler.categoryById(catId)+" category");
			drawAddDogManager(cw.catId);
		}
		
		private function drawAddDogManager (id:int=0):void
		{
			legendLabel.text = "Add dog in '"+handler.categoryById(id)+"' category";
		
			vb = null;
			vb_dog = new VBox(scrollDisplay)
			vb_dog.spacing = Layout.SIDE_MENU_GAP;
			
			d_cat = id;
			
			d_id = new NumericStepper(vb_dog);
			d_id.value = handler.numDogs+1;
			
			d_name = new InputText(vb_dog);
			
			d_doc = new InputText(vb_dog);
			
			d_birth = new InputText(vb_dog);
			
			d_owner = new InputText(vb_dog);
			
			d_add_button = new PushButton(vb_dog,0,0,"Add dog",checkAddDog)
			
			d_cancel_button = new PushButton(vb_dog,0,0,"Close window",clearAddDog)
			
		}
		
		private function checkAddDog (me:MouseEvent):void
		{
			if(handler.dogInfoByID(d_id.value)=={})
			{
				handler.dog_add(d_cat, d_id.value, d_name.text, d_owner.text, d_birth.text, d_doc.text);
				drawCategories();
			}
		}
		
		private function clearAddDog (me:MouseEvent):void
		{
			vb_dog = null;
			drawCategories();
		}
		
	}
}